package com.jcl.recruit.mobile.service;

import com.jcl.core.utils.StringUtils;
import com.jcl.recruit.core.config.SystemConfig;
import com.jcl.recruit.core.constants.Constants;
import com.jcl.core.web.utils.RequestUtils;
import com.jcl.recruit.corp.logic.SubcompanyLogic;
import com.jcl.recruit.corp.model.Subcompany;
import com.jcl.recruit.user.logic.UserExternalAccessLogic;
import com.jcl.recruit.user.logic.UserLogic;
import com.jcl.recruit.user.model.User;
import com.jcl.recruit.user.model.UserExternalAccess;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName MobileService

 * @Author yyliu
 * @Date 2018/9/19 9:24
 * @Version 1.0
 **/
@Service
public class MobileService {

    @Autowired
    private UserLogic userLogic;

    @Autowired
    private SubcompanyLogic subcompanyLogic;

    @Autowired
    private UserExternalAccessLogic userExternalAccessLogic;

    private static String photo_url = "/api/attachment/unset/get/";
    /* *
     * @Author yyliu
     * @Description //TODO 
     * @Date 9:54 2018/9/19
     * @Param []
     * @return java.util.Map<java.lang.String,java.lang.Object>
     **/
    public Map<String, Object> userDetail(String category){

        Map<String, Object> dataMap = new HashMap<>();

        String corpCode = RequestUtils.getCorpCode();
        String corpName = "";
        String mealPackage = "试用版";
        int days = Constants.MealPackage.default_day;
        Integer userMembers = 0;

        String userName = "";
        String email = "";
        String photoUrl = "";
        String roles = "";
        if(StringUtils.hasLength(corpCode)){
            Subcompany subcompany = subcompanyLogic.findOneByCode(corpCode);
            if(null != subcompany){
                corpName = subcompany.getFullname();
                String meal = subcompany.getMealPackage();
                if(!Constants.MealPackage.default_package.equals(meal)){
                    mealPackage = subcompany.getMealPackage();
                }
            }
            userMembers = userLogic.countMembers(corpCode);

        }
        
        String userId = RequestUtils.getUserId();
        User user = userLogic.findOneById(Integer.parseInt(userId));
        Integer userCorpSize = 0;
        if(null != user){
            UserExternalAccess userExternalAccess = userExternalAccessLogic.findOneByUserId(
                    Integer.parseInt(userId), category);
            if(null != userExternalAccess){
                userExternalAccess.getExternalKey();
                List<UserExternalAccess> countList = userExternalAccessLogic.findListByExternalKey(
                        userExternalAccess.getExternalKey(), category, null);
                userCorpSize = null != countList ? countList.size() - 1  : 0;
            }

            if(null != user.getPhotoId()){
                photoUrl = SystemConfig.getSystemWebUrl() + photo_url + user.getPhotoId();
            }
            userName = user.getRealname();
            email = user.getEmail();
            roles = user.getRoles();
            /*UserAccess userAccess = userAccessLogic.findOneByUserId(user.getId());
            if(null != userAccess){
                photoUrl = userAccess.getPhoto();
            }*/
        }

        dataMap.put("corpName", corpName);
        dataMap.put("mealPackage", mealPackage);
        dataMap.put("days", days);
        dataMap.put("userMembers", userMembers);
        dataMap.put("userCorpSize",userCorpSize);
        dataMap.put("photoUrl", photoUrl);
        dataMap.put("userName", userName);
        dataMap.put("email", email);
        dataMap.put("roles", roles);

        return dataMap;
    }

}
